#include <iostream>
#include <string.h>
using namespace std;

class String{
private:
    char *data;
    int length;

public:
    //构造函数
 String(const char*str="")
    {
        length=strlen(str);
        data=new char(strlen(str)+1);
        strcpy(data,str);
        cout<<"normal function"<<endl;
    }
     //析构函数
    ~String(){
        cout<<"delete function"<<endl;
    }//拷贝构造函数
    String(const String &other):
    length(other.length)
    {
    data=new char[length+1];
    strcpy(data,other.data);
    cout<<"copy function"<<endl;
    }
    //运算符重载函数
    String &operator=(const String & other){
        if(this!=&other){
            delete [] data;
            length =other.length;
            data=new char[length];
            strcpy(data,other.data);
            cout<<"深拷贝构造函数："<<data<<"新地址"<<(void*)data<<endl;
        }
        return *this;
    }
    void print () const{
        cout<<"内容"<<data<<"长度"<<length<<"数据地址"<<(void*)data<<endl;
    }
}; 
int main(int argc, char * argv[]){
    cout <<"====创建原始对象====="<<endl;
    String str1("hello");
    str1.print();

    cout<<"====拷贝构造函数====="<<endl;
    String str2=str1;
    str2.print();
    
    cout<<"====赋值操作====="<<endl;
    String str3("World");
    str3=str1;
    str3.print();
    cout <<"===main函数结束==="<<endl;
    return 0;
}

